import { createRouter, createWebHistory } from "vue-router";
import { getToken } from "../utils/token-utils";
import ContentTest from '../pages/ContentTest.vue'
import UploadRecipe from "../pages/UploadRecipe.vue";

import Category_homepage from "../pages/category/Category_homepage.vue";
import Search from "../pages/category/Search.vue";
import Small_category from "../pages/category/Small_category.vue";
import Keyword_search from "../pages/category/Keyword_search.vue";
import MyFavorite from "../pages/MyFavorite.vue";
import Home from "../pages/Home.vue";
import Recipe_datail from "../pages/Recipe_detail.vue"
import Login from "../pages/Login.vue";
import Register from "../pages/Register.vue";
import Profile from "../pages/profile.vue";


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "login",
    },
    //首页
    {
      name: "home",
      path: "/home",
      component: Home,
    },
    //无用页面
    {
      name: "contentTest",
      path: "/contenttest",
      component: Recipe_datail,
    },
    //分类页面
    {
      name: "category_homepage",
      path: "/category_homepage",
      component: Category_homepage,
    },
    //新增菜谱页面
    {
      name: "uploadRecipe",
      path: "/uploadrecipe",
      component: UploadRecipe,
    },
    //收藏页面
    {
      name: "myFavorite",
      path: "/myFavorite",
      component: MyFavorite
    },
    //详情页面
    {
      name: "recipe_datail",
      path: "/recipe_datail",
      component: Recipe_datail
    },
    //搜索页面
    {
      name: "search",
      path: "/search",
      component: Search
    },
    //菜谱列表
    {
      name: "small_category",
      path: "/small_category",
      component: Small_category
    },
    //登录
    {
      name: "login",
      path: "/login",
      component: Login
    },
    {
      name: "register",
      path: "/register",
      component: Register
    },
    {
      name: 'profile',
      path: '/profile',
      component: Profile
    },
    //关键词搜索
    {
      name: "keyword_search",
      path: "/keyword_search",
      component: Keyword_search
    }
  ],
  
});


//全局前置守卫
router.beforeEach((to, from, next) => {
  const token = getToken();
  if (token) {
    if (to.path == "/login" || to.path == "/register") {
      next({ path: "/" });
    } else {
      next();
    }
  } else {
    next();
  }
});


//全局后置守卫

export default router;
